/**
 * 锁定和解锁报名实习的学生
 * 
 * 实现功能：
 * 1.锁定和解锁已选实习的学生
 * 2.锁定和解锁未选实习的学生
 * 
 * /xingzheng/internship/lock
 */

 
const Students = require('../../../models/internshipStudents')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Sequelize = require('sequelize')
const Op = Sequelize.Op


const lock_student = async function(req, res) {    
    try {

        if ('year_id' in req.body === false) {
          $.sendMessage(res, 1015, '参数year_id不存在')
          return
        }

        if ('status' in req.body === false) {
          $.sendMessage(res, 1015, '参数status不存在')
          return
        }

        var handleAllStudent = true // 是否对所有学生做处理？
        var result = {} // 存放处理结果
        if ('student_id' in req.body === true) {
          handleAllStudent = false
        }

        if (parseInt(req.body.status) === 1 || parseInt(req.body.status) === 0) {
          if (handleAllStudent) {// 修改所有学生
            result = await Students.update(
              {
                is_locked: parseInt(req.body.status)
              },
              {
                where: {
                  student_year: req.body.year_id, // 年度id
                  company_id: {
                    [Op.ne]: ''
                  }
                }
              }
            )
          } else {// 修改指定的学生
            result = await Students.update(
              {
              is_locked: parseInt(req.body.status)
              },
              {
                where: {
                  student_year: req.body.year_id,
                  id: parseInt(req.body.student_id)
                }
              }
            )
          }
          // 返回结果
          res.json({
            res: result
          })

        } else {
          $.sendMessage(res, 1015, '参数status有误')
          return
        }
        
    }

    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '实习报名', '学生锁定', '失败')
        return
    }
}

module.exports = lock_student